/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.CalcadoController;
import controller.CategoriaController;
import controller.CorController;
import controller.FornecedorController;
import controller.NumeracaoController;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import model.AtributosMODEL;
import model.CalcadoMODEL;
import model.CategoriaMODEL;
import model.CorMODEL;
import model.FornecedorMODEL;
import model.NumeracaoMODEL;
import table.CellRenderer;
import table.TooltipCellRender;
import table.atributos.AtributosTable;

/**
 *
 * @author Junior Andrade
 */
public class Calcado extends javax.swing.JDialog {

    private static final int CALCADO = 0;
    private static final int ATRIBUTOS = 1;

    private int status = CALCADO;
    private int linhaSelecionada = -1;

//    private List<AtributosMODEL> atributos;
    

    private List<CorMODEL> cores;
    private List<NumeracaoMODEL> numeracoes;
    private List<FornecedorMODEL> fornecedores;
    private List<CategoriaMODEL> categorias;

    private JTable tblAtributos;
    private AtributosTable tableModel;
    private JScrollPane scrollPane;

    /**
     * Creates new form Tenis
     */
    public Calcado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);

        preencheListas();
        montaTabela();
        statusTela(status);
        preencheCombos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTValor = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDetalhes = new javax.swing.JTextArea();
        jCFornecedor = new javax.swing.JComboBox();
        jCCategoria = new javax.swing.JComboBox();
        jCGenero = new javax.swing.JComboBox();
        jBOk = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jCCor = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jBAdcionar = new javax.swing.JButton();
        jCNumeracao = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jTQuantidade = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jBRemover = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMGravar = new javax.swing.JMenu();
        jMLimpar = new javax.swing.JMenu();
        jMCancelar = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Formulário de Cadastro", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 51, 51))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Fornecedor:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Nome:");

        jTNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Categoria:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Genero:");

        jTValor.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Valor:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("Detalhes:");

        jTDetalhes.setColumns(20);
        jTDetalhes.setRows(5);
        jScrollPane1.setViewportView(jTDetalhes);

        jCFornecedor.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jCCategoria.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jCGenero.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jBOk.setText("OK");
        jBOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 61, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addComponent(jCFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jCCategoria, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jCGenero, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jTValor, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jBOk)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(27, 27, 27))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jCGenero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBOk)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jCCategoria, jCFornecedor});

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Atributos", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 51, 51))); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 103, Short.MAX_VALUE)
        );

        jCCor.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Cor:");

        jBAdcionar.setText("Adcionar");
        jBAdcionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAdcionarActionPerformed(evt);
            }
        });

        jCNumeracao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Numeração:");

        jTQuantidade.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Quantidade:");

        jBRemover.setText("Remover");
        jBRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRemoverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jCCor, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jCNumeracao, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTQuantidade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBRemover)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBAdcionar)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jCNumeracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jCCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jBAdcionar)
                        .addComponent(jBRemover))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8))
                        .addGap(27, 27, 27)))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jMenuBar1.setBackground(new java.awt.Color(51, 51, 51));
        jMenuBar1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jMGravar.setForeground(new java.awt.Color(255, 255, 255));
        jMGravar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Save.png"))); // NOI18N
        jMGravar.setText("Gravar");
        jMGravar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMGravar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMGravarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMGravar);

        jMLimpar.setForeground(new java.awt.Color(255, 255, 255));
        jMLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Undo.png"))); // NOI18N
        jMLimpar.setText("Limpar");
        jMLimpar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMLimpar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMLimparMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMLimpar);

        jMCancelar.setForeground(new java.awt.Color(255, 255, 255));
        jMCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/Back.png"))); // NOI18N
        jMCancelar.setText("Cancelar");
        jMCancelar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jMCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMCancelarMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMCancelar);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOkActionPerformed

        String nome = jTNome.getText();
        String genero = (String) jCGenero.getSelectedItem();
        String valor = jTValor.getText();
        String detalhe = jTDetalhes.getText();

        if (nome.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Erro Nome");
        } else if (jCFornecedor.getSelectedItem().equals("Cadastre um Fornecedor!")) {
            JOptionPane.showMessageDialog(this, "Cadastre o Fornecedor antes de Prosseguir!");
        } else if (jCCategoria.getSelectedItem().equals("Cadastre uma Categoria!")) {
            JOptionPane.showMessageDialog(this, "Cadastre uma Categoria antes de Prosseguir!");
        } else if (valor.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Erro Valor");
        } else if (detalhe.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Erro Detalhe");
        } else {
            statusTela(ATRIBUTOS);
        }
    }//GEN-LAST:event_jBOkActionPerformed

    private void jMCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMCancelarMouseClicked
        if (jMCancelar.isEnabled()) {
            novo();
        }
    }//GEN-LAST:event_jMCancelarMouseClicked

    private void jBAdcionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAdcionarActionPerformed
        String qtd = jTQuantidade.getText();

        if (jCCor.getSelectedItem().equals("Cadastre uma Cor!")) {
            JOptionPane.showMessageDialog(this, "Cadastre uma Cor antes de Prosseguir!");
        } else if (jCNumeracao.getSelectedItem().equals("Cadastre uma numeração!")) {
            JOptionPane.showMessageDialog(this, "Cadastre uma Numeração antes de Prosseguir!");
        } else if (qtd.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Preencha o campo Quantidade!");
        } else {
            int cor = cores.get(jCCor.getSelectedIndex()).getId();
            int numeracao = numeracoes.get(jCNumeracao.getSelectedIndex()).getId();
            AtributosMODEL atr = new AtributosMODEL(cor, numeracao, Integer.parseInt(qtd));

            int linha = tableModel.exists(atr);
            if (linha != -1) {
                tableModel.updateAtributo(linha, atr);
            } else {
                tableModel.addAtributo(atr);
            }

            limpaCampos();
        }
    }//GEN-LAST:event_jBAdcionarActionPerformed

    private void jBRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRemoverActionPerformed
        linhaSelecionada = tblAtributos.getSelectedRow();
        if (linhaSelecionada != -1) {
            tableModel.removeAtributo(linhaSelecionada);
        }
    }//GEN-LAST:event_jBRemoverActionPerformed

    private void jMLimparMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMLimparMouseClicked
        if (jMLimpar.isEnabled()) {
            limpaCampos();
        }
    }//GEN-LAST:event_jMLimparMouseClicked

    private void jMGravarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMGravarMouseClicked
        if (jMGravar.isEnabled()) {
            List<AtributosMODEL> atributos = tableModel.getAll();
            if (atributos.size() > 0) {
                String nome = jTNome.getText();
                String genero = (String) jCGenero.getSelectedItem();
                String valor = jTValor.getText();
                String detalhe = jTDetalhes.getText();
                int fornecedor = fornecedores.get(jCFornecedor.getSelectedIndex()).getId();
                int categoria = categorias.get(jCCategoria.getSelectedIndex()).getId();
                if (new CalcadoController().salvar(fornecedor, categoria, nome, genero,
                        Double.parseDouble(valor), detalhe, atributos) != -1) {
                    JOptionPane.showMessageDialog(this, "Cadastrado com sucesso!");
                } else {
                    JOptionPane.showMessageDialog(this, "Erro ao cadastrar!");
                }
            }
        }
    }//GEN-LAST:event_jMGravarMouseClicked

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdcionar;
    private javax.swing.JButton jBOk;
    private javax.swing.JButton jBRemover;
    private javax.swing.JComboBox jCCategoria;
    private javax.swing.JComboBox jCCor;
    private javax.swing.JComboBox jCFornecedor;
    private javax.swing.JComboBox jCGenero;
    private javax.swing.JComboBox jCNumeracao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMCancelar;
    private javax.swing.JMenu jMGravar;
    private javax.swing.JMenu jMLimpar;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTDetalhes;
    private javax.swing.JTextField jTNome;
    private javax.swing.JTextField jTQuantidade;
    private javax.swing.JTextField jTValor;
    // End of variables declaration//GEN-END:variables

    private void statusTela(int status) {
        if (status == CALCADO) {
            this.status = CALCADO;
            jCCor.setEnabled(false);
            jCNumeracao.setEnabled(false);
            jTQuantidade.setEditable(false);
            jBAdcionar.setEnabled(false);
            jBRemover.setEnabled(false);
            tblAtributos.setEnabled(false);

            jTNome.setEditable(true);
            jCFornecedor.setEnabled(true);
            jCCategoria.setEnabled(true);
            jCGenero.setEnabled(true);
            jTValor.setEditable(true);
            jTDetalhes.setEditable(true);
            jBOk.setEnabled(true);

        } else if (status == ATRIBUTOS) {
            this.status = ATRIBUTOS;
            jCCor.setEnabled(true);
            jCNumeracao.setEnabled(true);
            jTQuantidade.setEditable(true);
            jBAdcionar.setEnabled(true);
            jBRemover.setEnabled(true);
            tblAtributos.setEnabled(true);

            jTNome.setEditable(false);
            jCFornecedor.setEnabled(false);
            jCCategoria.setEnabled(false);
            jCGenero.setEnabled(false);
            jTValor.setEditable(false);
            jTDetalhes.setEditable(false);
            jBOk.setEnabled(false);

        }
    }

    private void limpaCampos() {
        if (status == CALCADO) {
            jTNome.setText("");
            jCFornecedor.setSelectedIndex(0);
            jCCategoria.setSelectedIndex(0);
            jCGenero.setSelectedIndex(0);
            jTValor.setText("");
            jTDetalhes.setText("");
            jCCor.setSelectedIndex(0);
            jCNumeracao.setSelectedIndex(0);
            jTQuantidade.setText("");
        } else if (status == ATRIBUTOS) {
            jCCor.setSelectedIndex(0);
            jCNumeracao.setSelectedIndex(0);
            jTQuantidade.setText("");
            tblAtributos.clearSelection();
            linhaSelecionada = -1;
        }
    }

    private void preencheCombos() {
        preencheComboFornecedor();
        preencheComboCategoria();
        preencheComboGenero();
        preencheComboCor();
        preencheComboNumeracao();

    }

    private void preencheListas() {
        
        fornecedores = new FornecedorController().listaFornecedores();
        
        categorias = new CategoriaController().listar();

        cores = new CorController().listar();

        numeracoes = new NumeracaoController().listar();
    }

    private void preencheComboFornecedor() {
        if (fornecedores.isEmpty()) {
            jCFornecedor.setEnabled(false);
            jCFornecedor.addItem("Cadastre um Fornecedor!");
        } else {
            jCFornecedor.removeAllItems();
            for (FornecedorMODEL forn : fornecedores) {
                jCFornecedor.addItem(forn.getRazaoSocial());
            }
        }
    }

    private void preencheComboCategoria() {
        if (categorias.isEmpty()) {
            jCCategoria.setEnabled(false);
            jCCategoria.addItem("Cadastre uma Categoria!");
        } else {
            jCCategoria.removeAllItems();
            for (CategoriaMODEL cate : categorias) {
                jCCategoria.addItem(cate.getCategoria());
            }
        }
    }

    private void preencheComboGenero() {

        jCGenero.removeAllItems();

        jCGenero.addItem("Masculino");
        jCGenero.addItem("Feminino");
        jCGenero.addItem("Unissex");

    }

    private void preencheComboCor() {
        if (cores.isEmpty()) {
            jCCor.setEnabled(false);
            jCCor.addItem("Cadastre uma Cor!");
        } else {
            jCCor.removeAllItems();
            for (CorMODEL cor : cores) {
                jCCor.addItem(cor.getCor());
            }
        }
    }

    private void preencheComboNumeracao() {
        if (numeracoes.isEmpty()) {
            jCNumeracao.setEnabled(false);
            jCNumeracao.addItem("Cadastre uma numeração!");
        } else {
            jCNumeracao.removeAllItems();
            for (NumeracaoMODEL num : numeracoes) {
                jCNumeracao.addItem("De: " + num.getDe() + " Até: " + num.getAte());
            }
        }
    }

    //METODOS DA TABELA
    private void montaTabela() {

        jPanel3.add(getTblAtributos());
        scrollPane.setBounds(5, 15, jPanel3.getWidth() - 10, jPanel3.getHeight() - 20);
    }

    private JScrollPane getTblAtributos() {
        if (tblAtributos == null) {
            tblAtributos = new JTable();
            // permite selecionar apenas uma linha de cada vez
            tblAtributos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            tblAtributos.setDefaultRenderer(String.class, new CellRenderer());

            tblAtributos.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent me) {
                    //        System.out.println(linhaSelecionada);
                    if ((me.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
                        if (tblAtributos.isEnabled()) {

//                            if (status == VISUALIZAR) {
//                                statusTela(SELECIONAR);
//
//                            }
                        }
                    } else if ((me.getModifiers() & MouseEvent.BUTTON2_MASK) != 0) {
                        JOptionPane.showMessageDialog(null, "Isso não é algo Legal!");
                    } else if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
                        JPopupMenu menu = new JPopupMenu();
                        JMenuItem cliqueme = new JMenuItem("Copiar");
                        int col = tblAtributos.columnAtPoint(me.getPoint());
                        int row = tblAtributos.rowAtPoint(me.getPoint());
                        cliqueme.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent ae) {
                                copyClipboard(String.valueOf(tableModel.get(row, col)));
                            }
                        });

                        menu.add(cliqueme);

                        menu.show(tblAtributos, me.getX(), me.getY());
                    }
                }

            });
            tblAtributos.setModel(getTableModel());
            defineRenderers();
            scrollPane = new JScrollPane(tblAtributos);
        }
        return scrollPane;
    }

    private AtributosTable getTableModel() {
        if (tableModel == null) {

            tableModel = new AtributosTable(cores, numeracoes);

        }
        return tableModel;
    }

    private void defineRenderers() {
//        DefaultTableCellRenderer rendererCentro = new DefaultTableCellRenderer();
//        rendererCentro.setHorizontalAlignment(SwingConstants.CENTER);
//        DefaultTableCellRenderer rendererDireita = new DefaultTableCellRenderer();
//        rendererDireita.setHorizontalAlignment(SwingConstants.RIGHT);
//        DefaultTableCellRenderer rendererEsquerda = new DefaultTableCellRenderer();
//        rendererEsquerda.setHorizontalAlignment(SwingConstants.LEFT);

        JTableHeader header = tblAtributos.getTableHeader();
        header.setPreferredSize(new Dimension(0, 35));
        TableColumnModel modeloDaColuna = tblAtributos.getColumnModel();
        int[] tam = {500, 250, 100, 80, 70, 60, 100, 200, 65, 100, 60, 50, 25, 40};

        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            modeloDaColuna.getColumn(i).setMaxWidth(tam[i]);
            modeloDaColuna.getColumn(i).setCellRenderer(new TooltipCellRender());

        }

    }

    private void copyClipboard(String texto) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection selection = new StringSelection(texto);
        clipboard.setContents(selection, null);
    }

    private void limiteJTextFields() {
//        jTRg.setDocument(new LimiteChar(12));
//        jTNome.setDocument(new LimiteChar(50));
//        jPSenha.setDocument(new LimiteChar(15));
//        jPSenha2.setDocument(new LimiteChar(15));
//        jTEmailPrinc.setDocument(new LimiteChar(50));
//        jTEmailSec.setDocument(new LimiteChar(50));
    }

    private void novo() {
        tableModel.removeAll();
        limpaCampos();
        statusTela(CALCADO);
        limpaCampos();
    }

}
